package Level02;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.DoubleStream;

/*
通过键盘录入五个人的姓名,每当录入一个人的姓名,随机给出一个成绩[0, 100](包含 0 分,也
包含 100 分).将姓名和成绩存储到 Map 集合中,姓名作为 key,成绩作为 value.统计五个学生的
总分,最高分,最低分,平均分,并将总分,最高分,最低分,平均分,写入到 score.txt 文件中,格式为:
总分=xxx,最高分=xxx,最低分=xxx,平均分=xxx
 */
public class Ten {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        HashMap<String, Double> hashMap = new HashMap<>();
        Random random = new Random();
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        for (int i = 1; i < 5 + 1; i++) {
            System.out.println("请输入第" + i + "个姓名");
            String nextLine = scanner.nextLine();
            double score = random.nextDouble() * 101;
            hashMap.put(nextLine, Double.parseDouble(decimalFormat.format(score)));
        }
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        double total = 0;
        Set<String> keySet = hashMap.keySet();
        for (String key : keySet) {
            Double aDouble = hashMap.get(key);
            //最高分和最低分
            if (max < aDouble) {
                max = aDouble;
            } else if (min > aDouble) {
                min = aDouble;
            }
            //总分
            total += aDouble;
        }
        //写入文件
        try {
            FileWriter writer = new FileWriter("score.txt");
            writer.write("总分=" + decimalFormat.format(total)
                    + ",最高分=" + max + ",最低分=" + min + "" +
                    ",平均分=" + decimalFormat.format(total / hashMap.size()));
            System.out.println("数据已写入score.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("总分=" + decimalFormat.format(total));
        System.out.println("最高分=" + max);
        System.out.println("最低分=" + min);
        System.out.println("平均分=" + decimalFormat.format(total / hashMap.size()));
    }
}

